﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Collections.Specialized;
using System.Configuration;

namespace KingSite.Library.Mail {
    public class MailHelper {
        static IMailSetting mailSetting = new MailSetting();

        public static IMail GetInstance() {
            string[] type = mailSetting.MailType.Split(',');
            string a = type[1];
            string n = type[0];
            Assembly ass = Assembly.Load(a);
            Type[] t = ass.GetTypes();
            object o = ass.CreateInstance(n);
            IMail result = (IMail)o;
            return result;
        }
    }

    public interface IMailSetting {
        string MailType { get; }

        #region
        bool EnabledSSL { get; }
        string Password { get; }
        int Port { get; }
        string SenderAddress { get; }
        string SenderName { get; }
        string Server { get; }
        string Username { get; }
        #endregion
    }


    public class MailSetting : IMailSetting {
        public string MailType {
            get { return ((NameValueCollection)ConfigurationManager.GetSection("KingSiteLibrary/Mail"))["MailType"]; }
        }

        #region

        public bool EnabledSSL {
            get {
                bool b = false;
                string s = ((NameValueCollection)ConfigurationManager.GetSection("KingSiteLibrary/Mail")).Get("EnabledSSL");
                if (!string.IsNullOrEmpty(s)) {
                    b = Convert.ToBoolean(s);
                }
                return b;
            }
        }

        public string Password {
            get {
                string s = string.Empty;
                s = ((NameValueCollection)ConfigurationManager.GetSection("KingSiteLibrary/Mail")).Get("Password");
                return s;
            }
        }

        public int Port {
            get {
                int port = 25;
                string s = ((NameValueCollection)ConfigurationManager.GetSection("KingSiteLibrary/Mail")).Get("Port");
                if (!string.IsNullOrEmpty(s)) {
                    port = Convert.ToInt32(s);
                }
                return port;
            }
        }

        public string SenderAddress {
            get {
                string s = string.Empty;
                s = ((NameValueCollection)ConfigurationManager.GetSection("KingSiteLibrary/Mail")).Get("SenderAddress");
                return s;
            }
        }

        public string SenderName {
            get {
                string s = string.Empty;
                s = ((NameValueCollection)ConfigurationManager.GetSection("KingSiteLibrary/Mail")).Get("SenderName");
                return s;
            }
        }

        public string Server {
            get {
                string s = string.Empty;
                s = ((NameValueCollection)ConfigurationManager.GetSection("KingSiteLibrary/Mail")).Get("Server");
                return s;
            }
        }

        public string Username {
            get {
                string s = string.Empty;
                s = ((NameValueCollection)ConfigurationManager.GetSection("KingSiteLibrary/Mail")).Get("Username");
                return s;
            }
        }

        #endregion
    }
}
